<script setup lang="ts">
import { flagOptions, timeOptions } from '@/services/constants'
import { uploadImage,gettoken,judgeDepartment } from '@/services/consult'
import { useConsultStore } from '@/stores'
import type { ConsultIllness, Image } from '@/types/consult'
import { showConfirmDialog, showToast } from 'vant'
import type {
  UploaderAfterRead,
  UploaderFileListItem
} from 'vant/lib/uploader/types'
import { computed, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

// 病情描述对象
const form = ref<ConsultIllness>({
  illnessDesc: '',
  illnessTime: undefined,
  consultFlag: undefined,
  pictures: []
})
const token = ref('')
onMounted(async () => {
  const res = await gettoken()
  token.value = res.token
})

// 上传图片
const fileList = ref<Image[]>([])
// 图片上传
const onAfterRead: UploaderAfterRead = (item) => {
  if (Array.isArray(item)) return
  if (!item.file) return
  item.status = 'uploading'
  item.message = '上传中...'
  uploadImage(item.file,token.value)
    .then((res) => {
      item.status = 'done'
      item.message = undefined
      item.url =  "http://sdeie5g85.hb-bkt.clouddn.com/"+res.key
      // 同步数据
      form.value.pictures?.push(item.url)
    })
    .catch(() => {
      item.status = 'failed'
      item.message = '上传失败'
    })
}

const onDeleteImg = (item: UploaderFileListItem) => {
  form.value.pictures = form.value.pictures?.filter(
    (pic) => pic.url !== item.url
  )
}

const disabled = computed(
  () =>
    !form.value.illnessDesc ||
    form.value.illnessTime === undefined ||
    form.value.consultFlag === undefined
)

const store = useConsultStore()
const router = useRouter()
const next = () => {
  if (!form.value.illnessDesc) return showToast('请输入病情描述')
  if (form.value.illnessTime === undefined)
    return showToast('请选择症状的持续时间')
  if (form.value.consultFlag === undefined) return showToast('请选择是否就诊过')
  // 记录病情
  store.setIllness(form.value)

  judgeDepartment(form.value).then((res) => {
    console.info(res)
        showConfirmDialog({
        title: '温馨提示',
        message: '根据您的描述将给您分到('+res.data+')科室，请确定？',
        closeOnPopstate: false
        }).then(() => {
        //   跳转，携带标识
        router.push('/consult/chat')
        })
    })
    .catch(() => {

    })


}

// 数据的回显
onMounted(() => {
  if (store.consult.illnessDesc) {
    showConfirmDialog({
      title: '温馨提示',
      message: '是否恢复之前填写的病情信息？',
      closeOnPopstate: false
    }).then(() => {
      // 回显数据
      const { illnessDesc, illnessTime, consultFlag, pictures } = store.consult
      form.value = { illnessDesc, illnessTime, consultFlag, pictures }
      fileList.value = pictures || []
    })
  }
})

</script>
<template>
    <div class="consult-illness-page">
      <cp-nav-bar title="智能导诊" />
      <!-- 医生提示 -->
      <div class="illness-tip van-hairline--bottom">
        请描述你的疾病或症状、是否用药、就诊经历，然后根据您的描述自动为您分配科室
      </div>
      <!-- 收集信息 -->
      <div class="illness-form">
        <van-field
          type="textarea"
          rows="3"
          placeholder="请详细描述您的病情，病情描述不能为空"
          v-model="form.illnessDesc"
        ></van-field>
        <div class="item">
          <p>本次患病多久了？</p>
          <cp-radio-btn :options="timeOptions" v-model="form.illnessTime" />
        </div>
        <div class="item">
          <p>此次病情是否去医院就诊过？</p>
          <cp-radio-btn :options="flagOptions" v-model="form.consultFlag" />
        </div>
        <!-- 上传组件 -->
        <div class="illness-img">
          <van-uploader
            upload-icon="photo-o"
            upload-text="上传图片"
            max-count="9"
            :max-size="5 * 1024 * 1024"
            v-model="fileList"
            :after-read="onAfterRead"
            @delete="onDeleteImg"
          ></van-uploader>
          <p class="tip" v-if="!fileList.length">
            上传内容仅医生可见,最多9张图,最大5MB
          </p>
        </div>
        <!-- 下一步 -->
        <van-button
          @click="next"
          :class="{ disabled }"
          type="primary"
          block
          round
        >
          下一步
        </van-button>
      </div>
    </div>
  </template>
  
  <style lang="scss" scoped>
  .consult-illness-page {
    padding-top: 46px;
    .van-button {
      font-size: 16px;
      margin-bottom: 30px;
      &.disabled {
        opacity: 1;
        background: #fafafa;
        color: #d9dbde;
        border: #fafafa;
      }
    }
  }
  .illness-img {
    padding-top: 16px;
    margin-bottom: 40px;
    display: flex;
    align-items: center;
    .tip {
      font-size: 12px;
      color: var(--cp-tip);
    }
    ::v-deep() {
      .van-uploader {
        &__preview {
          &-delete {
            left: -6px;
            top: -6px;
            border-radius: 50%;
            background-color: var(--cp-primary);
            width: 20px;
            height: 20px;
            &-icon {
              transform: scale(0.9) translate(-22%, 22%);
            }
          }
          &-image {
            border-radius: 8px;
            overflow: hidden;
          }
        }
        &__upload {
          border-radius: 8px;
        }
        &__upload-icon {
          color: var(--cp-text3);
        }
      }
    }
  }
  
  .illness-tip {
    display: flex;
    padding: 15px;
    .img {
      width: 52px;
      height: 52px;
      border-radius: 4px;
      overflow: hidden;
      margin-top: 10px;
    }
    .info {
      flex: 1;
      padding-left: 12px;
      .tit {
        font-size: 16px;
        margin-bottom: 5px;
      }
      .tip {
        padding: 12px;
        background: var(--cp-bg);
        color: var(--cp-text3);
        font-size: 13px;
        margin-bottom: 10px;
      }
      .safe {
        font-size: 10px;
        color: var(--cp-text3);
        display: flex;
        align-items: center;
        .cp-icon {
          font-size: 12px;
          margin-right: 2px;
        }
      }
    }
  }
  .illness-form {
    padding: 15px;
    .van-field {
      padding: 0;
      &::after {
        border-bottom: none;
      }
    }
    .item {
      > p {
        color: var(--cp-text3);
        padding: 15px 0;
      }
    }
  }
  </style>
  